package com.inhim.pjhim.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.TextView;

import com.inhim.pjhim.R;
import com.inhim.pjhim.entity.ProjectListEntity;
import com.inhim.pjhim.http.Urls;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;
import org.yczbj.ycrefreshviewlib.holder.BaseViewHolder;

import java.io.File;

import fm.jiecao.jcvideoplayer_lib.JCUtils;

public class VideoBottomDialogAdapter extends RecyclerArrayAdapter<ProjectListEntity.List> {

    private Activity context;
    private int mReaderId;
    private int mPosition;
    public VideoBottomDialogAdapter(Activity context) {
        super(context);
        this.context=context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        PersonViewHolder personViewHolder =new PersonViewHolder(parent,viewType);
        return personViewHolder;
    }

    public void setReaderId(int ReaderId){
        this.mReaderId=ReaderId;
    }

    public int getmPosition(){
        return mPosition;
    }

    public class PersonViewHolder extends BaseViewHolder<ProjectListEntity.List> {

        TextView tv_name;
        TextView tv_time;
        CheckBox checkbox_collect;
        RadioButton checkbox_download;
        View view_select;
        PersonViewHolder(ViewGroup parent,int position){
            super(parent, R.layout.item_radio_bottom_dialog);
            tv_name=getView(R.id.tv_name);
            tv_time=getView(R.id.tv_time);
            checkbox_collect=getView(R.id.checkbox_collect);
            checkbox_download=getView(R.id.checkbox_download);
            view_select=getView(R.id.view_select);
            /*addOnClickListener(R.id.checkbox_collect);
            addOnClickListener(R.id.checkbox_download);*/
            checkbox_collect.setOnClickListener(v -> {
                    if(mChildOnClickListener!=null){
                        mChildOnClickListener.onClickCollect(checkbox_collect,position);
                }
            });
            checkbox_download.setOnClickListener(v -> {
                if(mChildOnClickListener!=null){
                    mChildOnClickListener.onClickDownload(checkbox_download,position);
                }
            });
        }
        @Override
        public void setData(ProjectListEntity.List data) {
            super.setData(data);
            tv_name.setText(data.getTitle());
            if(data.getDuration()!=null && !"".equals(data.getDuration())){
                tv_time.setText(JCUtils.stringForTime2(Integer.valueOf(data.getDuration())));
            }
            checkbox_collect.setChecked(Boolean.valueOf(data.getCollectionStatus()));
            if(data.getUrl()!=null){
                String vedioName = data.getUrl().substring(data.getUrl().lastIndexOf("/"));
                String videoPath = Urls.getFilePath() + vedioName;
                File videoFile = new File(videoPath);
                checkbox_download.setChecked(videoFile.exists());
            }
            if(mReaderId==data.getReaderId()){
                mPosition=getAdapterPosition();
                view_select.setVisibility(View.VISIBLE);
            }else{
                view_select.setVisibility(View.GONE);
            }
        }
    }

    private ChildOnClickListener mChildOnClickListener;

    public void setChildOnClickListener(ChildOnClickListener childOnClickListener){
        this.mChildOnClickListener=childOnClickListener;
    }

    public interface ChildOnClickListener{
        void onClickCollect(CheckBox radioButton,int position);
        void onClickDownload(RadioButton radioButton,int position);
    }
}
